package com.phiture.erp.purchase.web.controller.admin.api;

import cn.hutool.core.collection.CollUtil;
import com.phiture.erp.purchase.api.pojo.req.ErpPurchaseReturnReq;
import com.phiture.erp.purchase.api.pojo.resp.ErpPurchaseReturnItemResp;
import com.phiture.erp.purchase.api.pojo.resp.ErpPurchaseReturnResp;
import com.phiture.erp.purchase.api.service.ErpPurchaseReturnApi;
import com.phiture.erp.purchase.core.service.ErpPurchaseReturnService;
import com.phiture.erp.purchase.dal.entity.ErpPurchaseReturnDO;
import com.phiture.erp.purchase.dal.entity.ErpPurchaseReturnItemDO;
import javafx.util.Pair;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author YH
 * @date 2025-06-12-15:49
 */

@Service
@RequiredArgsConstructor
public class ErpPurchaseReturnApiServiceImpl implements ErpPurchaseReturnApi {
    private final ErpPurchaseReturnService purchaseReturnService;

    @Override
    public List<ErpPurchaseReturnResp> getPurchaseReturnList(ErpPurchaseReturnReq req) {
        List<ErpPurchaseReturnDO> purchaseReturnList = purchaseReturnService.getPurchaseReturnList(req.getStatus(), req.getStartDate(), req.getEndDate());
        return CollUtil.isEmpty(purchaseReturnList) ? Collections.emptyList() : purchaseReturnList.stream().map(item -> {
            ErpPurchaseReturnResp resp = new ErpPurchaseReturnResp();
            resp.setPurchaseReturnId(item.getId());
            resp.setPurchaseReturnNo(item.getNo());
            resp.setPurchaseReturnStatus(item.getStatus());
            resp.setReturnTime(item.getReturnTime());
            resp.setSupplierId(item.getSupplierId());
            resp.setTotalCount(item.getTotalCount());
            resp.setTotalPrice(item.getTotalPrice());
            return resp;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ErpPurchaseReturnItemResp> getPurchaseReturnItemList(Map<Long, javafx.util.Pair<String, Long>> purchaseReturnMap) {
        List<ErpPurchaseReturnItemDO> purchaseReturnItemList = purchaseReturnService.getPurchaseReturnItemListByReturnIds(purchaseReturnMap.keySet());
        return CollUtil.isEmpty(purchaseReturnItemList) ? Collections.emptyList() : purchaseReturnItemList.stream().map(item ->{
            ErpPurchaseReturnItemResp itemResp = new ErpPurchaseReturnItemResp();
            itemResp.setPurchaseReturnId(item.getReturnId());
            Pair<String, Long> pair = purchaseReturnMap.get(item.getReturnId());
            if(pair != null){
                itemResp.setPurchaseReturnNo(pair.getKey());
                itemResp.setSupplierId(pair.getValue());
            }
            itemResp.setPurchaseReturnItemId(item.getId());
            itemResp.setOrderItemId(item.getOrderItemId());
            itemResp.setWarehouseId(item.getWarehouseId());
            itemResp.setMaterialId(item.getMaterialId());
            itemResp.setMaterialUnitId(item.getMaterialUnitId());
            itemResp.setMaterialPrice(item.getMaterialPrice());
            itemResp.setCount(item.getCount());
            itemResp.setTotalPrice(item.getTotalPrice());
            itemResp.setTaxPercent(item.getTaxPercent());
            itemResp.setTaxPrice(item.getTaxPrice());
            return itemResp;
        }).collect(Collectors.toList());
    }

    @Override
    public void backfillPurchaseReturnAccountCheckFlag(Collection<Long> purchaseReturnIds, Boolean accountCheckFlag) {
        purchaseReturnService.backfillPurchaseReturnAccountCheckFlag(purchaseReturnIds, accountCheckFlag);
    }

    @Override
    public void updatePurchaseReturnAccountCheckFlag(Collection<String> purchaseReturnNos, Boolean accountCheckFlag) {
        purchaseReturnService.updatePurchaseReturnAccountCheckFlag(purchaseReturnNos, accountCheckFlag);
    }
}
